import HttpService from '../../service/HttpService'
import ApiUrl from '../../service/ApiUrl'
import Util from '../../service/Util'

// mutations-type config
const Types = {
	GET_CROWDSUBJECT_REQUEST: 'GET_CROWDSUBJECT_REQUEST',
	GET_CROWDSUBJECT_SUCCESS: 'GET_CROWDSUBJECT_SUCCESS',
	GET_CROWDSUBJECT_FAILED: 'GET_CROWDSUBJECT_FAILED',
	GET_FUNCSUBJECT_REQUEST: 'GET_FUNCSUBJECT_REQUEST',
	GET_FUNCSUBJECT_SUCCESS: 'GET_FUNCSUBJECT_SUCCESS',
	GET_FUNCSUBJECT_FAILED: 'GET_FUNCSUBJECT_FAILED'
}

// init state
const state = {
	crowdSubjectList: [],
	funcSubjectList:[]
}

// get filter data from state
const getters = {
    crowdSubjectList: state => state.crowdSubjectList,
    funcSubjectList: state => state.funcSubjectList
}

const getSubjects = function(category,type){
	let opts = {
		apiObj : ApiUrl.getSubjectItems,
		noAuth: true,
		query:{
			category:category,
			mode:type
		}
	};
	return opts;
}

const allCategory = {
	id: '',
	name: '全部列表',
	icon: '/static/all-cate.png'
}

// action
const actions = {
	getCrowdSubjects({commit, state},type){
		let loading = Util.loading();
		loading.start();
		commit(Types.GET_CROWDSUBJECT_REQUEST)
		let opts = getSubjects('crowd',type);
		HttpService.accessAPI(opts, result => { 
			loading.close();
			result.unshift(allCategory);
			commit( Types.GET_CROWDSUBJECT_SUCCESS, {result})
		}, reason => { commit(Types.GET_CROWDSUBJECT_FAILED, { result }) });
	},
	getFuncSubjects({commit, state},type){
		let loading = Util.loading();
		loading.start();
		commit(Types.GET_FUNCSUBJECT_REQUEST)
		let opts = getSubjects('function',type);
		HttpService.accessAPI(opts, result => {
			console.log('result',result)
			loading.close();
			commit( Types.GET_FUNCSUBJECT_SUCCESS, {result})
		}, reason => { commit(Types.GET_FUNCSUBJECT_FAILED, { result }) });
	}
}

// init mutations 
const mutations = {
	[Types.GET_CROWDSUBJECT_REQUEST] ( state ){
		state.crowdSubjectList = [];
	},
	[Types.GET_CROWDSUBJECT_SUCCESS] ( state, { result } ){
		result.map(function(value){
			
			if( value.id ){
				if( value.icon ){
					value.icon += '?imageView2/2/w/100';
				}else{
					value.icon = '/static/placeholder.png';
				}
			}
		})
		state.crowdSubjectList = result.slice(0,8);
	},
	[Types.GET_CROWDSUBJECT_FAILED] ( state, { result } ){
		state.crowdSubjectList = result;
		console.log( 'mutations success', reason );
	},
	[Types.GET_FUNCSUBJECT_REQUEST] ( state ){
		state.funcSubjectList = [];
	},
	[Types.GET_FUNCSUBJECT_SUCCESS] ( state, { result } ){
		let arr = Object.values(result);
		arr.map(function(value){
			if(  value.banner ){
				value.banner += '?imageView2/2/w/100';
			}else{
				value.banner = '/static/placeholder.png';
			}
			value.goods.map(function(v){
				if(  v.cover ){
					v.cover += '?imageView2/2/w/100';
				}else{
					v.cover = '/static/placeholder2.png';
				}
			})
		})
		state.funcSubjectList = result;
		console.log( 'mutations success', result );
	},
	[Types.GET_FUNCSUBJECT_FAILED] ( state, { result } ){
		state.funcSubjectList = result;
		console.log( 'mutations success', reason );
	}
}

export default {
  state,
  getters,
  actions,
  mutations
}